///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZCHAR_H_
#define _BUOLA_ZZ_ZCHAR_H_

#include <buola/zz/zbase.h>
#include <buola/zz/if.h>
#include <buola/container/ccharset.h>
#include <buola/functors/predicates/char.h>

namespace buola { namespace zz {

template<typename tCh>
class ZChar : public ZBase<ZChar<tCh>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef tCh tType;
    };

    
    explicit ZChar(tCh pCh)
        :   mCh(pCh)
    {}
    
    ZChar(const ZChar &pRH)=default;
    ZChar(ZChar &&pRH)=default;
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx&,const tSk &pSk) const
    {
        skip_over(pB,pE,pSk);
        
        if(pB==pE) return false;
        
        if(*pB==mCh)
        {
            pAttr=mCh;
            ++pB;
            return true;
        }
        
        return false;
    }
    
private:
    tCh mCh;
};

class ZAnyChar : public ZBase<ZAnyChar>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef typename std::iterator_traits<tIt>::value_type tType;
    };

    ZAnyChar()=default;
    ZAnyChar(const ZAnyChar &pRH)=default;
    ZAnyChar(ZAnyChar &&pRH)=default;
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx&,const tSk &pSk) const
    {
        skip_over(pB,pE,pSk);
        
        if(pB==pE) return false;
        
        pAttr=*pB;
        ++pB;
        return true;
    }
};

template<typename tCh>
class ZCharSet : public ZBase<ZCharSet<tCh>>
{
public:
    typedef CCharSet<tCh> tSet;
    template<typename tIt>
    struct tAttribute
    {
        typedef tCh tType;
    };

    explicit ZCharSet(tCh pCh)
    {
        mSet.Add(pCh);
    }
    
    ZCharSet(tCh pFirst,tCh pLast)
    {
        mSet.Add(pFirst,pLast);
    }
    
    explicit ZCharSet(const std::basic_string<tCh> &pStr)
    {
        for(auto i=pStr.begin();i!=pStr.end();++i)
            mSet.Add(*i);
    }
    
    explicit ZCharSet(const tCh *pStr)
    {
        for(;*pStr;pStr++)
            mSet.Add(*pStr);
    }
    
    explicit ZCharSet(const tSet &pSet)
        :   mSet(pSet)
    {}
    
    explicit ZCharSet(tSet &&pSet)
        :   mSet(std::move(pSet))
    {}
    
    ZCharSet(const ZCharSet &pRH)
        :   mSet(pRH.mSet)
    {
    }
    
    ZCharSet(ZCharSet &&pRH)
        :   mSet(std::move(pRH.mSet))
    {
    }
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx&,const tSk &pSk) const
    {
        skip_over(pB,pE,pSk);
        
        if(pB==pE) return false;
        
        if(mSet.Test(*pB))
        {
            pAttr=*pB;
            ++pB;
            return true;
        }
        
        return false;
    }
    
private:
    tSet mSet;
};

template<typename tChar>
inline ZCharSet<tChar> chr(const std::basic_string<tChar> &pChars)
{
    return ZCharSet<tChar>(pChars);
}

template<typename tChar>
inline ZCharSet<tChar> chr(const tChar *pChars)
{
    return ZCharSet<tChar>(pChars);
}

template<typename tChar>
inline ZCharSet<tChar> chr(const CCharSet<tChar> &pCharSet)
{
    return ZCharSet<tChar>(pCharSet);
}

template<typename tChar>
inline ZCharSet<tChar> chr(CCharSet<tChar> &&pCharSet)
{
    return ZCharSet<tChar>(std::move(pCharSet));
}

template<typename tChar>
inline ZChar<tChar> chr(tChar pChar)
{
    return ZChar<tChar>(pChar);
}

inline ZAnyChar chr()
{
    return ZAnyChar();
}

inline auto space() -> decltype(zz::if_true(fn::is_ascii_space()))
{
    return zz::if_true(fn::is_ascii_space());
}

/*namespace zz*/ } /*namespace buola*/ }

#endif
